package com.augurit.wf.client.remind.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.PageUtils;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.StringUtils;

import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.Page;

import com.augurit.wf.bi.service.IWfBusInstanceService;
import com.augurit.wf.client.remind.convert.WfRemindConvertor;
import com.augurit.wf.client.remind.service.IWfRemindService;
import com.augurit.wf.client.remind.web.form.WfRemindForm;
import com.augurit.wf.common.dao.Jbpm4TaskDao;
import com.augurit.wf.common.dao.WfRemindDao;
import com.augurit.wf.common.entity.Jbpm4Task;
import com.augurit.wf.common.entity.WfRemind;
import com.augurit.wf.common.service.IWfService;

@Service
@Transactional
public class WfRemindServiceImpl implements IWfRemindService{

	@Autowired
	private WfRemindDao wfRemindDao;
	
	@Autowired
	private IWfService wfService;
	
	@Autowired
	private Jbpm4TaskDao jbpm4TaskDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public WfRemindForm get(Long id) {
		WfRemind entity = wfRemindDao.get(id);
		return WfRemindConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		wfRemindDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(WfRemindForm... forms) {
		if(forms != null)
			for(WfRemindForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WfRemindForm form){
		
		if(form != null){
			WfRemind entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = wfRemindDao.get(form.getId());
			}else{
				entity = new WfRemind();
			}
			
			//属性值转换
			WfRemindConvertor.convertFormToVo(form, entity);
			
			//保存
			wfRemindDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfRemindForm> search(Page<WfRemindForm> page, WfRemindForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuilder hql = new StringBuilder("from WfRemind ps where 1=1");
		List<Object> values = new ArrayList<Object>();
		
		// 查询条件
		if(form != null){
			if( StringUtils.isNotBlank(form.getWfInstanceId())){
				hql.append(" and ps.wfInstanceId = ?");
				values.add(form.getWfInstanceId());
				form.setActivityName(wfService.getActivityNameOfProcessInstance(form.getWfInstanceId()));
			}
			
			if( StringUtils.isNotBlank(form.getActivityName())){
				hql.append(" and ps.activityName = ?");
				values.add(form.getActivityName());
			}
			
			if( StringUtils.isNotBlank(form.getAssignee())){
				hql.append(" and ps.assignee = ?");
				values.add(form.getAssignee());
			}
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		wfRemindDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfRemindForm> list = WfRemindConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfRemindForm> search(Page<WfRemindForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<WfRemind> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		wfRemindDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfRemindForm> list = WfRemindConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 保存一条流程实例的催办记录（在保存前获取流程实例所处的流程结点）
	 * @param form
	 */
	public void saveRemind(WfRemindForm form){
		if(form != null && form.getWfInstanceId() != null){
			String activityName = wfService.getActivityNameOfProcessInstance(form.getWfInstanceId());
			form.setActivityName(activityName);
		}
		this.save(form);
	}
	
	/**
	 * 删除指定流程实例下的催办记录
	 * @param processInstanceId
	 */
	public void deleteRemindsByProcessInstanceId(String processInstanceId){
		if(processInstanceId != null){
			List<WfRemind> entities = wfRemindDao.getByProcessInstanceId(processInstanceId);
			
			if(entities != null && entities.size() > 0){
				wfRemindDao.delete(entities);
			}
		}
	}
	
	/**
	 * 获取提醒者
	 * @param processInstanceId
	 * @return
	 */
	@Transactional(readOnly = true)
	public Map<String, String> getReminder(String processInstanceId){
//		return jbpm4TaskDao.getAssignees(processInstanceId);
		return null;
	}
}
